API: Add Accept-Language echoer, useful for JS that wants to grab this (not available...
authorRoan Kattouw <catrope@users.mediawiki.org>
Mon, 7 Jun 2010 18:53:30 +0000 (18:53 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Mon, 7 Jun 2010 18:53:30 +0000 (18:53 +0000)
includes/api/ApiQueryUserInfo.php

index 3f4903b..5fa4059 100644 (file)
@@ -121,8 +121,48 @@ class ApiQueryUserInfo extends ApiQueryBase {
                                $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
                        }
                }
+               
+               if ( isset( $this->prop['acceptlang'] ) ) {
+                       $vals['acceptlang'] = $this->getAcceptLang();
+                       $result->setIndexedTagName( $vals['acceptlang'], 'lang' );
+               }
                return $vals;
        }
+       
+       protected function getAcceptLang() {
+               // Modified version of code found at http://www.thefutureoftheweb.com/blog/use-accept-language-header
+               if ( !isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
+                       return array();
+               }
+               
+               // Break up string into pieces (languages and q factors)
+               $lang_parse = null;
+               preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0(\.[0-9]+))?)?/i',
+                       $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse );
+               
+               if ( !count( $lang_parse[1] ) ) {
+                       return array();
+               }
+               // Create a list like "en" => 0.8
+               $langs = array_combine( $lang_parse[1], $lang_parse[4] );
+               // Set default q factor to 1
+               foreach ( $langs as $lang => $val ) {
+                       if ( $val === '' ) {
+                               $langs[$lang] = 1;
+                       }
+               }
+               // Sort list
+               arsort( $langs, SORT_NUMERIC );
+               
+               // Format for API output
+               $retval = array();
+               foreach ( $langs as $lang => $val ) {
+                       $r = array( 'q' => $val );
+                       ApiResult::setContent( $r, $lang );
+                       $retval[] = $r;
+               }
+               return $retval;
+       }
 
        protected function getRateLimits() {
                global $wgUser, $wgRateLimits;
@@ -174,6 +214,7 @@ class ApiQueryUserInfo extends ApiQueryBase {
                                        'editcount',
                                        'ratelimits',
                                        'email',
+                                       'acceptlang',
                                )
                        )
                );
@@ -192,6 +233,7 @@ class ApiQueryUserInfo extends ApiQueryBase {
                                '  editcount  - adds the current user\'s edit count',
                                '  ratelimits - lists all rate limits applying to the current user',
                                '  email      - adds the user\'s email address and email authentication date',
+                               '  acceptlang - echoes the Accept-Language header sent by the client in a structured format',
                        )
                );
        }